var chooseLocation = requirePlugin('chooseLocation');
let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_default: 0,
    areaInfo: '',
    province_id: '',
    city_id: '',
    area_id: '',
    address_info:[],
    user_address_id:'',
    showadd: true,
    showaddress: false,
    address_list:[],
    formto: 0,
    true_name_show: true,
    mob_phone_show: true,
    address_show: true,
    true_name: '',
    mob_phone: '',
    address: '',
	genderIndex: 0,
	genders: ['先生', '女士'],
	navigationAddress: '',
	latitude: '',
	longitude: '',
	statusBarHeight: app.globalData.statusBarHeight,
  },
	onShow() {
		const selectedLocation = chooseLocation.getLocation();
		if (selectedLocation) {
			this.setData({ 
				navigationAddress: selectedLocation.address,
				latitude: selectedLocation.latitude,
				longitude: selectedLocation.longitude,
			});
		  
		}
	},
	 // 自定义事件 用来接收子组件传递的数据的
	  handleItemChange(e){
	      // 接收传递过来的参数
	      //const index=e.detail.index;
	      const {index}=e.detail;
		  if(index) {
			  wx.navigateBack(-1)
		  } else {
			  this.setData({
			    showaddress: (index),
			    showadd: (!index)
			  })
		  }
	  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new app.ToastPannel();
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var user_address_id = options.user_address_id
    var fromto = options.fromto
    if (fromto != 1) {
      fromto = 0
    }
    
    that.address(0)
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_User&met=address&act=edit&typ=json',
      data: { k: key, u: uid, id: user_address_id },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      }, // 设置请求的 header
      success: function (res) {
        
        var address_info = res.data.data.address_list
        if(res.data.status == 200){
            that.setData({
              address_info: address_info,
              user_address_id: address_info.id,
              province_id: address_info.user_address_province_id,
              city_id: address_info.user_address_city_id,
              area_id: address_info.user_address_area_id,
              areaInfo: address_info.user_address_area,
              is_default: address_info.user_address_default,
              fromto: fromto,
              true_name:address_info.user_address_contact,
              mob_phone:address_info.user_address_phone,
              address: address_info.user_address_address,
			  genderIndex: address_info.user_address_gender ? address_info.user_address_gender : 0 ,
			  navigationAddress: address_info.navigationAddress,
			  latitude: address_info.latitude,
			  longitude: address_info.longitude,
            })
        }
      },
      fail: function (res) {
        that.show('数据加载失败')
      }
    })

  },
  //设置默认发货地址
  switch_change: function (e) {
    var is_default = e.detail.value
    if (is_default) {
      this.setData({
        is_default: 1
      })
    }else{
      this.setData({
        is_default: 0
      })
    }
  },

  formSubmit: function (e) {
    var that = this
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var true_name = e.detail.value.true_name
    var mob_phone = e.detail.value.mob_phone
    var area_info = that.data.areaInfo
    var address = e.detail.value.address
    if (true_name.length == 0) {
      that.show('请输入收货人姓名')
    } else if (mob_phone.length == 0) {
      that.show('请输入收货人手机号码')
    } else if (!myreg.test(mob_phone)) {
      that.show('手机号格式有误')
    } else if (area_info.length == 0) {
      that.show('请输入所在地区')
    } else if (address.length == 0) {
      that.show('请输入详细地址')
    } else if (that.data.navigationAddress == '') {
      that.show('请选择导航地址')
    } else {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_User&met=editAddressInfo&typ=json',
        data: { 
          k: key, 
          u: uid, 
          user_address_contact: true_name, 
          user_address_phone: mob_phone,
           province_id: that.data.province_id, 
           city_id: that.data.city_id, 
           area_id: that.data.area_id, 
           user_address_address: address, 
           address_area: area_info, 
           user_address_default: that.data.is_default, 
           user_address_id: that.data.user_address_id,
		   user_address_gender: that.data.genderIndex,
		   navigationAddress: that.data.navigationAddress,
		   latitude: that.data.latitude,
		   longitude: that.data.longitude
            },
        method: 'POST', 
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        }, // 设置请求的 header
        success: function (res) {
         
          if (res.data.status == 200) {
            that.show('编辑成功')
            if (that.data.fromto == 1) {
              wx.redirectTo({
                url: '../address_select/address_select',
              })
            } else {
              wx.redirectTo({
                url: '../address_list/address_list',
              })
            }
          } else {
            that.show('编辑失败')
          }
        },
        
      })
    }

  },

// 性别选择变化
	  genderChange(e) {
		this.setData({ genderIndex: e.detail.value[0] })
	  },
	  navigationAddress: function() {
		  const key = "DEHBZ-VTH3L-ZU7PV-MHG4B-IHHTE-FVB5G"; // 替换为实际Key
		  const referer = "铧佗帮护";
		  wx.navigateTo({
		    url: `plugin://chooseLocation/index?key=${key}&referer=${referer}`
		  });
	  },
  //选择地区
  area_info: function () {
    var that = this;
    that.setData({
      showaddress: (!that.data.showaddress),
      showadd: (!that.data.showadd)
    })
    if (that.data.showaddress == true) {
      that.address(0)
    }
  },
  //获取一级地区
  address: function (e) {
    var that = this
    if (e == 0) {
      var id = 0
      var areaInfo = ''
      that.setData({
        areaInfo: areaInfo
      })

    } else {
      var id = e.currentTarget.dataset.id
    }

    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Base_District&met=district&typ=json',
      method: 'GET',
      data: {
        pid: id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
       
        if (res.data.status == 200) {
          var areaInfo = that.data.areaInfo
          if (e != 0) {
            if (e.currentTarget.dataset.level == 0) {
              areaInfo = areaInfo + " " + e.currentTarget.dataset.name
              that.setData({
                province_id: id
              })
            } else if (e.currentTarget.dataset.level == 0) {
              areaInfo = areaInfo + " " + e.currentTarget.dataset.name
              that.setData({
                city_id: id
              })

            } else if (e.currentTarget.dataset.level == 0) {
              areaInfo = areaInfo + " " + e.currentTarget.dataset.name
              that.setData({
                area_id: id
              })

            }
            that.setData({
              areaInfo: areaInfo
            })

            if (res.data.data.items.length == 0 || e.currentTarget.dataset.level == 3) {
              that.setData({
                showaddress: (!that.data.showaddress),
                showadd: (!that.data.showadd)
              })
            }
          }

          that.setData({
            address_list: res.data.data.items,
          })
        }
      }
    })
  },
  //收货人
  true_name: function (e) {
    var that = this
    if (e.detail.value) {
      that.setData({
        true_name_show: true,
        true_name: e.detail.value
      })
    } else {
      that.setData({
        true_name_show: false
      })
    }
  },
  //手机号码
  mob_phone: function (e) {
    var that = this
    if (e.detail.value) {
      that.setData({
        mob_phone_show: true,
        mob_phone: e.detail.value
      })
    } else {
      that.setData({
        mob_phone_show: false
      })
    }
  },
  //详细地址
  addressinput: function (e) {
    var that = this
    if (e.detail.value) {
      that.setData({
        address_show: true,
        address: e.detail.value
      })
    } else {
      that.setData({
        address_show: false
      })
    }

  },

  //收货人
  true_name_clear: function (e) {
    var that = this
    that.setData({
      true_name_show: false,
      true_name: ''
    })
  },
  //手机号码
  mob_phone_clear: function () {
    var that = this
    that.setData({
      mob_phone_show: false,
      mob_phone: ''
    })
  },
  //详细地址
  address_clear: function (e) {
    var that = this
    that.setData({
      address_show: false,
      address: ''
    })

  }

})